//
//  MainController.m
//  庐山通
//
//  Created by mac22 on 14/10/19.
//  Copyright (c) 2014年 郑朝军. All rights reserved.
//

#import "MainController.h"
#import "Grouble.h"
#import "MJRefresh.h"
#import "ZCJHomeCell.h"
#import "MyDataFrame.h"
#import "LeftSlideViewController.h"
#import "LeftSortsViewController.h"
#import "SelectFindHttp.h"
#import "UITableView+JFTableView.h"
#import "SelectAddressView.h"
#import "AddressScrollView.h"
#import "GetPlistArray.h"
#import "FirstableViewCell.h"
#import "TwoTableViewCell.h"
#import "ThreeTableViewCell.h"
#import "FourTableViewCell.h"
#import "FiveTableViewCell.h"
#import "GetUrlString.h"
#import "RushDataModel.h"
#import "RushDealsModel.h"
#import "DiscountModel.h"
#import "SixViewCell.h"
#import "RecommentModel.h"
#import "HomeTopViewController.h"
#import "BuyWebViewController.h"
#import "DisCountWebViewController.h"
#import "MBProgressHUD+MJ.h"
#import "GesterLikeViewController.h"
#import "HomeMoveViewController.h"
#import "KTVSViewController.h"
#import "ArroundTravelViewController.h"
#import "BackWebViewController.h"
#import "HotlesViewController.h"

@interface MainController()<MJRefreshBaseViewDelegate,SelectAddressViewDelegate,ChangeCityButtonDelegate,AddressScrollViewButtonDelegate,UITableViewDataSource,UITableViewDelegate,FirstableViewCellDelegate,TwoTableViewCellDelegate,ThreeTableViewCellDelegate>
@property (nonatomic, strong)MJRefreshFooterView *foot;
@property (nonatomic, strong)MJRefreshHeaderView *head;

@property (nonatomic,strong)NSMutableArray * decaticFrame;

@property (nonatomic, strong)SelectAddressView *selectAddressView ;
@property (nonatomic, strong)UIButton *leftButton;
@property(nonatomic, strong)UITableView *firstTableView;
@property (nonatomic, strong)NSArray *menuArray;

@end
@implementation MainController{
    NSMutableArray *_rushArray;
    NSMutableArray *_discountArray;
    NSMutableArray *_recommentArray;
}


- (NSMutableArray *)decaticFrame
{
    if (_decaticFrame == nil) {
        _decaticFrame = [[NSMutableArray alloc]init];
        
    }
    return _decaticFrame;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setUpTableView];
    // 1.设置导航栏的内容
    [self setupNavBar];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initArrayData];
    [self setRush];
   
    
    
}
- (void)dealloc
{
    [self.foot free];
    [self.head free];
}
- (void)initArrayData
{
    self.menuArray =  [GetPlistArray arrayWithString:@"menuData.plist"];
   // ZCJLog(@"%@",self.menuArray);
    _rushArray  = [NSMutableArray array];
    _discountArray = [NSMutableArray array];
    _recommentArray = [NSMutableArray array];
    

}
-(void)setUpTableView{
    self.firstTableView = [UITableView initWithTableView:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64) withDelegate:self];
    self.firstTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.firstTableView];
}
- (void)setRush
{
    MJRefreshHeaderView * head = [[MJRefreshHeaderView alloc]init];
    head.scrollView = self.firstTableView;
    head.delegate = self;
    [head beginRefreshing];
    self.head = head;
    MJRefreshFooterView * foot = [[MJRefreshFooterView alloc]init];
    foot.scrollView = self.firstTableView;
    foot.delegate = self;
    self.foot = foot;
}
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if ([refreshView isKindOfClass:[MJRefreshFooterView class]]) { // 上拉刷新
        [self loadMoreData];
    } else { // 下拉刷新
        [self loadNewData];
    }
}
- (void)loadMoreData
{
    ZCJLog(@"jjloadMoreDatajjj");
    [self.foot endRefreshing];
}
- (void)loadNewData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self loadFirstData];
        [self laodTwoData];
        [self lastData];
    });
}
// 加载最后一个cell
- (void)lastData
{
    NSString * lastUrl = [[GetUrlString sharedManager]urlWithRecomment];
    [[GetUrlString sharedManager]urlWithVisitAd];
    //ZCJLog(@"%@",lastUrl);
    [SelectFindHttp getWirhUrl:lastUrl parms:nil success:^(id json) {
        [_recommentArray removeAllObjects];
        //ZCJLog(@"%@",json);
        NSMutableArray * array = [json objectForKey:@"data"];
        //ZCJLog(@"%@",array);
        for (int i = 0; i < array.count; i ++) {
            RecommentModel * recomment = [RecommentModel objectWithKeyValues:array[i]];
            [_recommentArray addObject:recomment];
        }
        [self.firstTableView reloadData];
    } failture:^(id error) {
        ZCJLog(@"%@",error);
    }];
}
- (void)laodTwoData
{
    NSString *urlStr = [[GetUrlString sharedManager]urlWithDiscount];
    [SelectFindHttp getWirhUrl:urlStr parms:nil success:^(id json) {
         NSMutableArray *dataDic = [json objectForKey:@"data"];
        [_discountArray removeAllObjects];
        for (int i = 0; i < dataDic.count; i++) {
            DiscountModel *discount = [DiscountModel objectWithKeyValues:dataDic[i]];
            [_discountArray addObject:discount];
        }
        //ZCJLog(@"%@",_discountArray);
        [self.firstTableView reloadData];
    } failture:^(id error) {
        ZCJLog(@"%@",error);
    }];
}
// 加载第一个cell的数据
- (void)loadFirstData
{
     NSString *urlStr = [[GetUrlString sharedManager]urlWithRushBuy];
    [SelectFindHttp getWirhUrl:urlStr parms:nil success:^(id json) {
        NSDictionary *dataDic = [json objectForKey:@"data"];
        RushDataModel *rushDataM = [RushDataModel objectWithKeyValues:dataDic];
        
        [_rushArray removeAllObjects];
        for (int i = 0; i < rushDataM.deals.count; i++) {
            RushDealsModel *deals = [RushDealsModel objectWithKeyValues:rushDataM.deals[i]];
            [_rushArray addObject:deals];
        }
        [self.head endRefreshing];
        [self.firstTableView reloadData];

    } failture:^(id error) {
        
        ZCJLog(@"%@",error);
    }];
}
- (void)setupNavBar
{
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftButton = leftBtn;
    leftBtn.frame = CGRectMake(0, 0, 40, 35);
    [leftBtn setImage:[UIImage imageNamed:@"icon_homepage_upArrow"] forState:UIControlStateNormal];
    [leftBtn setImage:[UIImage imageNamed:@"icon_homepage_downArrow"] forState:UIControlStateSelected];
    //先设置按钮里面的内容居中
    leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    //设置文字居左 －>向左移35
    [leftBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -35, 0, 0)];
    //设置文字居左 －>向右移30
    leftBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
    [leftBtn setTitle:@"武汉" forState:UIControlStateNormal];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [leftBtn addTarget:self action:@selector(btn_leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    
    UIButton *MapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    MapBtn.frame = CGRectMake(0, 0, 50, 35);
    [MapBtn setImage:[UIImage imageNamed:@"icon_homepage_map_old"] forState:UIControlStateNormal];
    [MapBtn addTarget:self action:@selector(mapBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:MapBtn];
    
    
    UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    searchBar.backgroundImage = [UIImage imageNamed:@"icon_homepage_search"];
    searchBar.placeholder = @"搜索";
    self.navigationItem.titleView = searchBar;
    
    // 搞一个继承自view的东西，里面弄一个view遮盖屏幕设置它的透明度，然后搞一个scrollerView
    self.selectAddressView =   [[SelectAddressView alloc]initWithFrame:CGRectMake(0, 0 ,ScreenWidth, ScreenHeight)];
    self.selectAddressView.delegate = self;
    self.selectAddressView.addressScrollView.delegate = self;
    self.selectAddressView.addressScrollView.changeCityDelegate = self;
    
    [self.view addSubview:self.selectAddressView];
    self.selectAddressView.hidden = YES;

}
// 点击左边的按钮
- (void)btn_leftBtnClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
    self.selectAddressView.hidden =  !self.selectAddressView.hidden;
}
// selectAddressView代理
-(void)removeMaskView
{
    
}
// 
-(void)changeCityButtonClick:(UIButton *)button
{
    
}

- (void)mapBtnClick
{
   
    ZCJLog(@"地图导航");
}
#pragma mark- delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //ZCJLog(@"%d",_recommentArray.count);
    return 5 + _recommentArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        FirstableViewCell * cell = [FirstableViewCell cellWithTableView:tableView menuArray:self.menuArray];
        cell.delegate = self;
        return cell;
    }else if(indexPath.row == 1){
        TwoTableViewCell * cell = [TwoTableViewCell TwoTableViewCell:tableView];
        cell.delegate = self;
        if (_rushArray.count != 0) {
            [cell setRushData:_rushArray];
        }
        
        return cell;
    }else if (indexPath.row == 2){
        ThreeTableViewCell * cell = [ThreeTableViewCell ThreeTableViewCell:tableView];
        cell.delegate = self;
        if (_discountArray.count != 0) {
            cell.discountArray = _discountArray;
            
        }
        return cell;
    }else if (indexPath.row == 3){
        FourTableViewCell * cell = [FourTableViewCell FourTableViewCell:tableView];
        return cell;
    }else if (indexPath.row == 4){
        FiveTableViewCell * cell = [FiveTableViewCell FiveTableViewCell:tableView];
        return cell;
    }
    else{
        SixViewCell * cell = [SixViewCell SixViewCell:tableView];
//        cell.recommentModel =
        if (_recommentArray.count!=0) {
            cell.recommentModel = _recommentArray[indexPath.row-5];
            
        }
        return cell;
    }
  
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row >= 5) {
        GesterLikeViewController * gesterLike = [[GesterLikeViewController alloc]init];
        RecommentModel * recommentModel = _recommentArray[indexPath.row-5];
   
        gesterLike.ppoid  =[recommentModel.id stringValue];
        ZCJLog(@"%@",gesterLike.ppoid);
        
        [self.navigationController pushViewController:gesterLike animated:YES];
    }
    
   // [MBProgressHUD showSuccess:@"zcj还在开发中......."];
    ZCJLog(@"%@",indexPath);
}

#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 180;
    }else if(indexPath.row == 1){
        return 120;
    }else if (indexPath.row == 2){
        return 160;
    }else if (indexPath.row == 3){
        return 100;
    }else if (indexPath.row == 4){
        return 60;
    }else {
        return 120;
    }
}
#pragma mark - cell的代理方法
- (void)homeMenuCellClick:(long)sender
{
    // 美食
    if (sender == 10) {
        HomeTopViewController * topView = [[HomeTopViewController alloc]init];
        
        topView.modalTransitionStyle = UIModalTransitionStylePartialCurl;
        [self.navigationController pushViewController:topView animated:YES];
    }else
    if (sender == 11) {// 电影
        HomeMoveViewController * homeMoveVC  = [[HomeMoveViewController alloc]init];
        [self.navigationController pushViewController:homeMoveVC animated:YES];
    }else
    // 酒店
    if (sender == 12) {
        HotlesViewController * homeMoveVC  = [[HotlesViewController alloc]init];
        homeMoveVC.name = @"酒店";
        [self.navigationController pushViewController:homeMoveVC animated:YES];
    }else
    // ktv
    if (sender == 13) {
        KTVSViewController * ktv = [[KTVSViewController alloc]init];
        [self.navigationController pushViewController:ktv animated:YES];
    }else if(sender == 14){
        BackWebViewController * backWebView = [[BackWebViewController alloc]init];
        backWebView.urlStr = @"http://m.z.jd.com/new.html";
        [self.navigationController pushViewController:backWebView animated:YES];
    }else if (sender == 15){
        ArroundTravelViewController * arroundTravel = [[ArroundTravelViewController alloc]init];
        [self.navigationController pushViewController:arroundTravel animated:YES];
    }
    
    ZCJLog(@"homeMenuCellClick:%ld",sender);
}
#pragma mark - 第二个cell的点击方法
-(void)didSelectAtIndexRushBuyCell:(long )index
{
    BuyWebViewController * buyWebShop = [[BuyWebViewController alloc]init];
    [self.navigationController pushViewController:buyWebShop animated:YES];
    ZCJLog(@"didSelectAtIndexRushBuyCell:%ld",index);
}
#pragma mark - 酒店特惠房，，等等点击之后的
-(void)didSelectAtIndexDiscountCell:(NSString *)urlStr
{
    NSString *urlString =  [[GetUrlString sharedManager]urlWithDiscountWebData:urlStr];
    ZCJLog(@"%@",urlString);
    
    DisCountWebViewController * webController = [[DisCountWebViewController alloc]init];
    webController.webStr = urlString;
    webController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    
    [self.navigationController pushViewController:webController animated:YES];
}

@end
